package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.IndustryInfoMapper;
import cc.rengu.igas.bomp.common.entity.IndustryInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 行业类别信息表操作接口实现类
 * Created by 王成 on 2020/3/10
 */
public class IndustryInfoMapperImpl implements IndustryInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertIndustryInfo(IndustryInfo industryInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertIndustryInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, industryInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记行业类别信息失败,industryType:<{}>,industryDesc:<{}>,id:<{}>,iReturnCode:<{}>", industryInfo.getIndustryType(), industryInfo.getIndustryDesc(), industryInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateIndustryInfoById(IndustryInfo industryInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateIndustryInfoById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, industryInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新行业类别信息失败,industryType:<{}>,industryDesc:<{}>,id:<{}>,iReturnCode:<{}>", industryInfo.getIndustryType(), industryInfo.getIndustryDesc(), industryInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public IndustryInfo selectIndustryInfoById(String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectIndustryInfoById");
        IndustryInfo industryInfo = new IndustryInfo();
        industryInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, industryInfo, industryInfo);
        if (0 != iReturnCode) {
            rglog.error("获取行业类别信息失败,id:<{}>,iReturnCode:<{}>", id, iReturnCode);
            return null;
        }
        return industryInfo;
    }

    @Override
    public IndustryInfo selectIndustryInfoByIndustryType(String industryType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectIndustryInfoByIndustryType");
        IndustryInfo industryInfo = new IndustryInfo();
        industryInfo.setIndustryType(industryType);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, industryInfo, industryInfo);
        if (0 != iReturnCode) {
            rglog.error("获取行业类别信息失败,industryType:<{}>,iReturnCode:<{}>", industryType, iReturnCode);
            return null;
        }
        return industryInfo;
    }

    @Override
    public List<IndustryInfo> selectIndustryInfoByKeys(String managementScope, String industryCode, String industryName, String industryDesc) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectIndustryInfoByKeys");
        IndustryInfo industryInfo = new IndustryInfo();
        industryInfo.setGrpNo(managementScope);
        industryInfo.setIndustryType(industryCode);
        industryInfo.setIndustryName(industryName);
        industryInfo.setIndustryDesc(industryDesc);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, industryInfo, IndustryInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据经营范围、行业编号、行业名称、行业描述查询行业类别信息失败,industryCode:<{}>,industryName:<{}>,industryDesc:<{}>.", industryCode, industryName, industryDesc);
            return null;
        }
        return objectList.stream().map(item -> (IndustryInfo) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectIndustryInfoPageByKeys(String managementScope, String industryCode, String industryName, String industryDesc, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectIndustryInfoByKeys");
        IndustryInfo industryInfo = new IndustryInfo();
        industryInfo.setGrpNo(managementScope);
        industryInfo.setIndustryType(industryCode);
        industryInfo.setIndustryName(industryName);
        industryInfo.setIndustryDesc(industryDesc);

        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, industryInfo, IndustryInfo.class, pageInfo);
        if (null == pageResult) {
            rglog.error("根据经营范围、行业编号、行业名称、行业描述查询行业类别信息失败,industryCode:<{}>,industryName:<{}>,industryDesc:<{}>.", industryCode, industryName, industryDesc);
            return null;
        }
        return pageResult;
    }
}
